package com.example.service;


import cn.hutool.core.date.DateTime;
import com.example.common.enums.ResultCodeEnum;
import com.example.entity.Bed;
import com.example.exception.CustomException;
import com.example.mapper.BedMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 挂号单业务处理
 **/
@Service
public class BedService {

    @Resource
    private BedMapper BedMapper;

    /**
     * 新增
     */
    public void add(Bed Bed) {
        Bed dbBed = BedMapper.selectByBedId(Bed.getBedId());
        if (dbBed != null) {
            throw new CustomException(ResultCodeEnum.BED_NUMBER_REPEAT);
        }
//        //随机生成一个挂号单的订单表号
//        Bed.setBedId(String.valueOf(System.currentTimeMillis()));
        BedMapper.insert(Bed);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        BedMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            BedMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Bed Bed) {
        Bed dbBed = BedMapper.selectByBedId(Bed.getBedId());
        if (dbBed != null && !dbBed.getId().equals(Bed.getId())) {
            throw new CustomException(ResultCodeEnum.BED_NUMBER_REPEAT);
        }
        BedMapper.updateById(Bed);
    }

    /**
     * 根据ID查询
     */
    public Bed selectById(Integer id) {
        return BedMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Bed> selectAll(Bed Bed) {
        return BedMapper.selectAll(Bed);
    }

    /**
     * 分页查询
     */
    public PageInfo<Bed> selectPage(Bed bed, Integer pageNum, Integer pageSize) {
        System.out.println(bed);
        PageHelper.startPage(pageNum, pageSize);
        List<Bed> list = BedMapper.selectAll(bed);
        return PageInfo.of(list);
    }

    /**
     * 根据医生的username进行分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<Bed> selectPage(String departmentName,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Bed> list = BedMapper.selectByDepartmentName(departmentName);
        System.out.println(list);
        return PageInfo.of(list);
    }
}